﻿using System;

namespace Moki
{
    /// <summary>
    /// Specifies number of calls.
    /// </summary>
    public class Times
    {
        internal Times(Func<int, bool> conditionChecker)
        {
            ConditionChecker = conditionChecker;
        }

        internal Func<int, bool> ConditionChecker { get; set; }

        public static Times Once()
        {
            return Times.Exactly(1);
        }

        public static Times Between(int a, int b)
        {
            AssertIsNonNegative(a);
            AssertIsNonNegative(b);
            AssertBetween(a, b);
            return new Times(x => (a <= x && x <= b));
        }

        public static Times Exactly(int i)
        {
            AssertIsNonNegative(i);
            return new Times(x => x == i);
        }

        public static Times AtLeast(int i)
        {
            AssertIsNonNegative(i);
            return new Times(x => x >= i);
        }

        public static Times AtMost(int i)
        {
            AssertIsNonNegative(i);
            return new Times(x => x <= i);
        }

        internal static void AssertBetween(int a, int b)
        {
            if (b >= a)
                throw new ArgumentException(String.Format("Value {1} should be greater than {0}", a, b));
        }

        internal static void AssertIsNonNegative(int i)
        {
            if (i < 0)
                throw new ArgumentException(String.Format("Value {0} should be non negative", i));
        }
    }
}
